package cn.nextProject.service.impl;

import cn.nextProject.entity.UserRoleGroup;
import cn.nextProject.mapper.UserRoleGroupMapper;
import cn.nextProject.service.UserRoleGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class UserRoleGroupServiceImpl implements UserRoleGroupService {

    @Autowired
    private UserRoleGroupMapper userRoleGroupMapper;

    @Cacheable(cacheNames = "userRoleGroup",
            key = "(#roleGroupId != null ? #roleGroupId : '') + '_' + " +
                    "(#username != null ? #username : '') + '_' + " +
                    "(#realname != null ? #realname : '') + '_'"
    )
    @Override
    public UserRoleGroup findOne(Long roleGroupId, String username,String realname) {
        return userRoleGroupMapper.findOne(roleGroupId,username,realname);
    }

    @CacheEvict(cacheNames = {"userRoleGroup","userRoleGroups","user","userList"},allEntries = true,beforeInvocation = true)
    @Override
    public void saveBatch(List<UserRoleGroup> userRoleGroupList) {
        if (!CollectionUtils.isEmpty(userRoleGroupList)){
            userRoleGroupMapper.saveBatch(userRoleGroupList);
        }
    }

    @CacheEvict(cacheNames = {"userRoleGroup","userRoleGroups","user","userList"},allEntries = true,beforeInvocation = true)
    @Override
    public void delete(Long id) {
        userRoleGroupMapper.delete(id);
    }
}
